package com.spider.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.spider.domain.Datasource;
import com.spider.service.DatasourceServiceI;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/dsc")
public class DataSourceController extends  BaseController{

    @Resource(name = "datasourceServiceI")
    private DatasourceServiceI dsi;

    @RequestMapping("/index")
    public String index(){
        return prefix + "/system/dataSource/database" + suffix;
    }
    @RequestMapping("/addPage")
    public String addPage(){
        return prefix + "/system/dataSource/addDataSource" + suffix;
    }

    @RequestMapping("/editPage/{id}")
    public String editPage(@PathVariable(name = "id") int id, ModelMap mmap){
        Datasource ds = dsi.getById(id);
        if(ds!=null){
            mmap.addAttribute("ds",ds);
        }
        return prefix + "/system/dataSource/editDataSource" + suffix;
    }

    @RequestMapping("/edit")
    @ResponseBody
    public Map<String,Object> edit(Datasource datasource){
        boolean b = dsi.saveOrUpdate(datasource);
        Map<String,Object> map = new HashMap<>();
        map.put("msg","编辑"+(b?"成功":"失败"));
        map.put("is",b);
        return map;
    }

    @RequestMapping("/add")
    @ResponseBody
    public Map<String,Object> add(Datasource datasource){
        boolean b = dsi.saveOrUpdate(datasource);
        Map<String,Object> map = new HashMap<>();
        map.put("msg","新增"+(b?"成功":"失败"));
        map.put("is",b);
        return map;
    }

    @RequestMapping("/remove/{id}")
    @ResponseBody
    public Map<String,Object> remove(@PathVariable(name = "id") int id){
        boolean b = dsi.removeById(id);
        Map<String,Object> map = new HashMap<>();
        map.put("msg","删除"+(b?"成功":"失败"));
        map.put("is",b);
        return map;
    }


    @RequestMapping("/test")
    @ResponseBody
    public Map<String,Object> test(Datasource datasource){
        return dsi.test(datasource);
    }



    @RequestMapping("/find")
    @ResponseBody
    public Map<String,Object> find(Datasource ds){
        QueryWrapper<Datasource> qw = new QueryWrapper<>();
        List<Datasource> list = dsi.list(qw);
        Map<String,Object> map = new HashMap<>();
        map.put("code",0);
        map.put("count",list.size());
        map.put("data",list);
        map.put("msg","数据加载完成");
        return map;
    }

    @RequestMapping("/getTableName/{id}")
    @ResponseBody
    public Map<String,Object> getTableName(@PathVariable(value="id") int id){
        Datasource ds = dsi.getById(id);
        Map<String,Object> map = new HashMap<>();
        map.put("data",new ArrayList<>());
        if(ds!=null){
            List<String> tableNames = dsi.getTableName(ds);
            map.put("data",tableNames);
        }

        return map;
    }
}
